﻿using System;

public class Mapper048 : MapperDefault
{
    private int irq_counter;
    private bool irq_enabled;

    public override void init(NES nes)
    {
        base.init(nes);
        this.reset();
    }

    public override void loadROM(ROM rom)
    {
        if (!rom.Valid)
        {
            Console.Out.WriteLine("VRC4: Invalid ROM! Unable to load.");
        }
        else
        {
            int num = rom.RomBankCount * 2;
            this.load8kRomBank(0, 0x8000);
            this.load8kRomBank(1, 0xa000);
            this.load8kRomBank(num - 2, 0xc000);
            this.load8kRomBank(num - 1, 0xe000);
            this.loadCHRROM();
            this.loadBatteryRam();
            base.nes.Cpu.requestIrq(2);
        }
    }

    public override void reset()
    {
        this.irq_enabled = false;
        this.irq_counter = 0;
    }

    public override int syncH(int scanline)
    {
        if ((this.irq_enabled && ((base.ppu.scanline & 0x18) != 0)) && ((scanline >= 0) && (scanline <= 0xef)))
        {
            if (this.irq_counter == 0)
            {
                this.irq_counter = 0;
                this.irq_enabled = false;
                return 3;
            }
            this.irq_counter++;
        }
        return 0;
    }

    public override void write(int address, short value_Renamed)
    {
        if (address < 0x8000)
        {
            base.write(address, value_Renamed);
        }
        else
        {
            switch (address)
            {
                case 0xc000:
                    this.irq_counter = value_Renamed;
                    return;

                case 0xc001:
                case 0xc002:
                case 0xe001:
                case 0xe002:
                    this.irq_enabled = value_Renamed != 0;
                    return;

                case 0xe000:
                    if ((value_Renamed & 0x40) == 0)
                    {
                        base.nes.Ppu.Mirroring = 0;
                        return;
                    }
                    base.nes.Ppu.Mirroring = 1;
                    return;

                case 0x8000:
                    this.load8kRomBank(value_Renamed, 0x8000);
                    return;

                case 0x8001:
                    this.load8kRomBank(value_Renamed, 0xa000);
                    return;

                case 0x8002:
                    this.load2kVromBank(value_Renamed * 2, 0);
                    return;

                case 0x8003:
                    this.load2kVromBank(value_Renamed * 2, 0x800);
                    return;

                case 0xa000:
                    this.load1kVromBank(value_Renamed, 0x1000);
                    return;

                case 0xa001:
                    this.load1kVromBank(value_Renamed, 0x1400);
                    return;

                case 0xa002:
                    this.load1kVromBank(value_Renamed, 0x1800);
                    return;

                case 0xa003:
                    this.load1kVromBank(value_Renamed, 0x1c00);
                    return;
            }
        }
    }
}

